package com.youdianstar.app.ui

import androidx.databinding.ObservableField
import androidx.lifecycle.viewModelScope
import com.youdianstar.app.common.base.BaseViewModel
import com.youdianstar.app.common.event.NetStateLiveData
import com.youdianstar.app.support.ApiRepository
import kotlinx.coroutines.launch


/**
 * File: BusinessJoinViewModel
 * Author: Assistant Create: 2024/12/19
 * Changes (from 2024/12/19)
 * --------------------------------------------------
 * description: 招商加盟ViewModel
 * ---------------------------------------------------
 *
 */
class BusinessJoinViewModel(private val repo: ApiRepository) : BaseViewModel() {

    val nameField = ObservableField<String>("")
    val phoneField = ObservableField<String>("")
    val cityField = ObservableField<String>("")
    val intentionField = ObservableField<String>("")
    val showSuccess = ObservableField<Boolean>(false)

    val submitResultLiveData = NetStateLiveData<Any>()

    /**
     * 提交表单
     */
    fun submitForm() {
        val name = nameField.get()?.trim() ?: ""
        val phone = phoneField.get()?.trim() ?: ""
        val city = cityField.get()?.trim() ?: ""
        val intention = intentionField.get()?.trim() ?: ""

        // 表单验证
        if (name.isEmpty() || phone.isEmpty() || city.isEmpty()) {
            toastEvent.postValue("请填写完整信息")
            return
        }

        // 调用接口
        showLoading()
        viewModelScope.launch {
            repo.submitBusinessJoin(name, phone, city, intention, submitResultLiveData)
        }
    }

    /**
     * 返回
     */
    fun backAction() {
        finish()
    }

}
